#!/usr/bin/python -O

from bottle import Bottle, request, template, run
import requests
import socket

hostname = socket.getfqdn()

app = Bottle()

SQREST = "http://{0}:9000/sq".format(hostname)

@app.get("/sq")
def hello():
    uid = request.query.get("uid")
    if uid is None:
        uid = "Jason"
    qstr = "{0}/social_id?uid={1}".format(SQREST, uid)

    resp = requests.get(qstr)

    idlist = [user['social_id'] for user in resp.json()['users']]

    sid = request.query.get("sid")

    if sid is None:
        sid = ','.join(idlist)

    qstr = "{0}/message_bysocial/{1}".format(SQREST, sid)

    resp = requests.get(qstr)

    messages = resp.json()["messages"]

    return template('social_queen', uid=uid, idlist=idlist, messages=messages)

import sys

port = 8008
if len(sys.argv) > 1:
    port = int(sys.argv[1])

run(app, host=hostname, port=port, debug=True)

